<!-- src/views/system/organization/OrganizationTree.vue -->
<script lang="ts" setup>
import type {
  OnActionClickParams,
  VxeTableGridOptions,
} from '#/adapter/vxe-table';
import type { OrgApi } from '#/api/system/org';

import type { VxeGridProps } from '#/adapter/vxe-table';
import { Page, useVbenModal } from '@vben/common-ui';
import { Plus } from '@vben/icons';
import { Button, message } from 'ant-design-vue';
import type { VbenFormProps } from '#/adapter/form';

import { useVbenVxeGrid } from '#/adapter/vxe-table';

import { getOrgList, deleteOrg } from '#/api/system/org';
import { $t } from '#/locales';
import { useColumns } from './data';
import Form from './modules/form.vue';
const [FormModal, formModalApi] = useVbenModal({
  connectedComponent: Form,
  destroyOnClose: true,
});

const formOptions: VbenFormProps = {
  // 默认展开
  collapsed: true,
  schema: [
    {
      component: 'Input',
      componentProps: {
        placeholder: 'Please enter 组织名称',
      },
      defaultValue: undefined,
      fieldName: 'oname',
      label: '组织名称',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: 'Please enter 组织层级',
      },
      fieldName: 'level',
      label: '组织层级',
    },
    {
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          {
            label: '集团',
            value: '1',
          },
          {
            label: '公司',
            value: '2',
          },
          {
            label: '部门',
            value: '3',
          },
        ],
        placeholder: '公司',
      },
      fieldName: 'otype',
      label: '组织类型',
    },
    {
      component: 'DatePicker',
      fieldName: 'createTime',
      label: '创建时间',
    },
  ],
  // 控制表单是否显示折叠按钮
  showCollapseButton: true,
  submitButtonOptions: {
    content: '查询',
  },
  // 是否在字段值改变时提交表单
  submitOnChange: false,
  // 按下回车时是否提交表单
  submitOnEnter: false,
};

/**
 * 编辑部门
 * @param row
 */
function onEdit(row: OrgApi.Org) {
  formModalApi.setData(row).open();
}
/**
 * 添加下级部门
 * @param row
 */
function onAppend(row: OrgApi.Org) {
  formModalApi.setData({ pono: row.ono }).open();
}
/**
 * 创建新组织
 */
function onCreate() {
  formModalApi.setData(null);
  formModalApi.open();
}

/**
 * 表格操作按钮的回调函数
 */
function onActionClick({ code, row }: OnActionClickParams<OrgApi.Org>) {
  switch (code) {
    case 'append': {
      onAppend(row);
      break;
    }
    case 'delete': {
      onDelete(row);
      break;
    }
    case 'edit': {
      onEdit(row);
      break;
    }
  }
}
/**
 * 删除部门
 * @param row
 */
function onDelete(row: OrgApi.Org) {
  const hideLoading = message.loading({
    content: $t('ui.actionMessage.deleting', [row.oname]),
    duration: 0,
    key: 'action_process_msg',
  });
  deleteOrg(row.oid)
    .then(() => {
      message.success({
        content: $t('ui.actionMessage.deleteSuccess', [row.oname]),
        key: 'action_process_msg',
      });
      refreshGrid();
    })
    .catch(() => {
      hideLoading();
    });
}

const [Grid, gridApi] = useVbenVxeGrid({
  gridEvents: {},
  formOptions,
  gridOptions: {
    checkboxConfig: {
      highlight: true,
      labelField: 'oid',
    },
    columns: useColumns(onActionClick),
    height: 'auto',
    keepSource: true,
    pagerConfig: {
      enabled: false,
    },
    proxyConfig: {
      ajax: {
        query: async (_params) => {
          return await getOrgList();
        },
      },
    },
    toolbarConfig: {
      custom: true,
      export: false,
      refresh: true,
      zoom: true,
    },
    treeConfig: {
      parentField: 'pono',
      rowField: 'oid',
      transform: false,
    },
  } as VxeTableGridOptions,
});

/**
 * 刷新表格
 */
function refreshGrid() {
  gridApi.query();
}
</script>

<template>
  <Page auto-content-height>
    <FormModal @success="refreshGrid" />
    <Grid table-title="组织机构树">
      <template #toolbar-tools>
        <Button type="primary" @click="onCreate">
          <Plus class="size-5" />
          {{ $t('ui.actionTitle.create', [$t('system.org.name')]) }}
        </Button>
      </template>
    </Grid>
  </Page>
</template>
